/**
 * 定义月报表模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.common.report.reportFormBase', ['water.entity.monitorData',
	                                                                        'OnlineMonitorDataReportService',
	                                                                          'water.common.dateUtil']);
	
	module.controller('commonReportFormController',function($scope,$rootScope,$monitorDataService,$controller,$dateUtil,$DataReportService){

		/**
		 * 开始时间
		 */
		$scope.startDate;
		/**
		 * 界面上冻结表格的组件
		 */
		$scope.freezeGrid;
		$scope.dataAPIName;
		$scope.dataExportAPIName;
		/**
		 * 统计的属性
		 */
		$scope.statisticsField = ['maxData','minData','avgData'];
		/**
		 * 水质类别属性
		 */
		$scope.waterStandardField = ['totalWaterStandard','mainPoll'];
		
		/**
		 * 监听左侧列表选择
		 */
		$scope.oNdataPramChanged = $scope.$on('dataPramChanged', function(event, station) {
			$scope.onStationSelectedBeforeQuery(station);
			var queryObject = $scope.getQueryObject();
			$scope.getReport(queryObject);
	    });
		
		/**
		 * 切换菜单的时候，把注册的事件取消掉
		 */
		$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
			$scope.oNdataPramChanged();
	 	});
		
		/**
		 * 查询按钮事件
		 */
		$scope.onSearchClick = function(){
			var queryObject = $scope.getQueryObject();
			$scope.getReport(queryObject);
		}
		/**
		 *  导出excel
		 */
		$scope.onExportExcelClick = function(){
			var queryObj = $scope.getQueryObject(true);
			$monitorDataService[$scope.dataExportAPIName](queryObj);
		}
		
		/**
		 * 站点选择后，报表查询前执行的逻辑
		 * 用于准备查询前的参数
		 */
		$scope.onStationSelectedBeforeQuery = function(station){
			$scope.selectedModel = station;
			$scope.parmList = $scope.selectedModel.parmToCheckList;
		}
		
		/**
		 * 查询报表
		 */
		$scope.getReport = function(queryObject){
			$monitorDataService[$scope.dataAPIName](queryObject).then(function(data){
				$scope.data = data;
			});
		}
		
		/**
		 * 获取查询参数
		 */
		$scope.getQueryObject = function(){
			
			var parmsStr = $scope.selectedModel.parmToChecksStr;
			var queryObject = {
					"MN":$scope.selectedModel.MN,
					"PARMS":parmsStr,
					"START":$scope.startDate + " 00:00:00",
					"END":$scope.startDate + " 23:59:59"
				};
			return queryObject;
		}
		
		/**
		 * 图表选项卡点击事件
		 */
		$scope.onTBClick = function(){
			for(var i=0;i<$scope.parmList.length;i++){
				if(i <= 3){
					$scope.parmList[i].checked = true;
				} else {
					$scope.parmList[i].checked = false;
				}
			}
			_showChar();
		}
		
		function _init(){
			$scope.startDate = $dateUtil.formatDate(new Date(),'yyyy-MM-dd');
		}
		
		/**
		 * 图表视图，勾选参数事件
		 */
		$scope.checkedParm = function($event,parmIndex){
			var checkedCount = 0;
			var checkBox = $event.target;
			for(var i=0;i<$scope.parmList.length;i++){
				if($scope.parmList[i].checked){
					checkedCount++;
				}
			}
			if(checkBox.checked && checkedCount >=4){
				checkBox.checked = false;
				$.messager.alert("提示","只能选择4个参数","info");
				return;
			}
			$scope.parmList[parmIndex].checked = checkBox.checked;
			_showChar();
		}
		
		/**
		 * 展示图表
		 */
		function _showChar(){
			var dataObject= {};
			dataObject.mn = $scope.selectedModel.MN;
			dataObject.statisType=$scope.statisType;
			var parmsToChar = [];
			for(var i=0;i<$scope.parmList.length;i++){
				if($scope.parmList[i].checked){
					parmsToChar.push($scope.parmList[i]);
				}
			}
			if($scope.data){
				//在绘制
				$DataReportService.showChar_for_new($scope.data.data,'char_tb',parmsToChar,$scope.data.pollInfo,dataObject);
			}
			
		}
		
		$("#listdata").idTabs();
		$("#lb").click();
		_init();
	});
	
}(window, angular));